package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.SysDictType;
import org.example.service.SysDictTypeService;

import java.util.List;

public class SysDictTypeListener implements ReadListener<SysDictType> {

    private static final  int BACH_COUNT =3;

    private List<SysDictType> cacheDataList =ListUtils.newArrayListWithExpectedSize(BACH_COUNT);

    private SysDictTypeService sysDictTypeService;

    public SysDictTypeListener(SysDictTypeService sysDictTypeService) {
        this.sysDictTypeService=sysDictTypeService;
    }

    @Override
    public void invoke(SysDictType sysDictType, AnalysisContext analysisContext) {

        System.out.println("解析出来:"+sysDictType);
        cacheDataList.add(sysDictType);

        if(cacheDataList.size()>=BACH_COUNT){

            sysDictTypeService.insertBach(cacheDataList);
            //清空
           cacheDataList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("解析完成");
        if(cacheDataList.size()>=1){
            System.out.println("list:"+cacheDataList);
            sysDictTypeService.insertBach(cacheDataList);
        }
    }
}
